﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using LuaC;

namespace LuaBoard
{
    public static class LuaListView
    {
        public static LuaItem rootluaitem;
        public static LuaBoard _main;
        private static ListView lv;
        private static TreeView tv;
        public static TabControl _MainTabs;
        public static TreeNode rootnode;
        public static TreeNode NowNode;
        

        public static void setup(LuaBoard main, ListView listview, TreeView treeview)
        {
            
            tv = treeview;
            lv = listview;
            lv.Clear();

            _main = main;
            string path = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, @"root");
            DirectoryInfo dir = new DirectoryInfo(path);
            if (!dir.Exists)
            {
                return;
            }
            rootluaitem = new LuaItem("root", dir, null, null, true);
            rootluaitem.sublp = new List<LuaItem>();
            TreeNode tn = new TreeNode(dir.Name);
            tn.Tag = rootluaitem;

            //int deep = 0;
            //LoadRootLoopDir(dir, tn, rootluaitem, deep);
            tv.Nodes.Add(tn);
            rootnode = tn;
            NowNode = tn;
        }
        public static void LoadRootLoopDir(DirectoryInfo dir, TreeNode tn, LuaItem lip,int deep)
        {
            deep++; if (deep > 1) return;
            if (dir.Name != "root")
            {
                LuaItem luaitemUpTree = new LuaItem("..", dir, lip, null, false);
                luaitemUpTree.UpTreeNode = lip.UpTreeNode;
                luaitemUpTree.ImageIndex = 0;
                lip.sublp.Add(luaitemUpTree);
            }
            
            DirectoryInfo[] dirs;
            TreeNode aNode;
            dirs = dir.GetDirectories();
            if (dirs.Length > 0)
                foreach (DirectoryInfo d in dirs)
                {

                    bool isHidden = ((d.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);

                    if (isHidden) continue;

                    FileInfo[] files = d.GetFiles("*.lua");

                    if (files.Length >= 1)
                    {
                        LuaItem luaitem = CreateLuaItem(d, lip, null);
                        AddImageList(luaitem);
                        if (luaitem.IsFolder == true)
                        {
                            aNode = new TreeNode(luaitem.Text);
                            luaitem.sublp = new List<LuaItem>();
                            luaitem.tn = aNode;
                            luaitem.UpTreeNode = tn;
                            luaitem.ImageIndex = 1;
                            aNode.Tag = luaitem;

                            LoadRootLoopDir(d, aNode, luaitem,deep);
                            tn.Nodes.Add(aNode);
                        }
                        lip.sublp.Add(luaitem);
                    }
                    else //Group
                    {
                        aNode = new TreeNode(d.Name);
                        LuaItem luaitem = new LuaItem(d.Name, d, lip, null, true);
                        luaitem.sublp = new List<LuaItem>();
                        luaitem.tn = aNode;
                        luaitem.UpTreeNode = tn;
                        luaitem.ImageIndex = 1;
                        lip.sublp.Add(luaitem);
                        aNode.Tag = luaitem;
                        
                        LoadRootLoopDir(d, aNode, luaitem,deep);
                        tn.Nodes.Add(aNode);
                    }
                }
        }
        public static void AddListViewItem(LuaItem lp)
        {
            ListViewItem item = lp.lviTag;
            lv.Items.Add(item);
        }
        public static LuaItem CreateLuaItem(DirectoryInfo d, LuaItem Parent, bool? IsShortcut)
        {
            LuaItem luaitem = new LuaItem(d.Name, d, Parent, IsShortcut, false);
            luaitem.Luac.LuaMsgEvent += new EventHandler<LuaClassMsgEventArgs>(_main.lua_LuaMsgEvent);
            luaitem.Luac.LuaProgressBarEvent += new EventHandler<LuaClassProgressBarEventArgs>(_main.lua_LuaProgressBarEvent);
            luaitem.ImageIndex = 2;
            luaitem.LoadLuaSystem();
            luaitem.LoadLuaToLuaVM(d);

            return luaitem;
        }
        public static void AddImageList(LuaItem lp)
        {
            if (lp.RelatedFile != null)
            {
                if (File.Exists(lp.RelatedFile) && !lv.LargeImageList.Images.ContainsKey(lp.RelatedFile))
                {
                    string ext = Path.GetExtension(lp.RelatedFile).ToUpper();

                    System.Drawing.Icon icon = Win32API.ExtractIcon(lp.RelatedFile, lp.RelatedFileIconIndex);

                    lv.SmallImageList.Images.Add(lp.RelatedFile, icon.ToBitmap());
                    lv.LargeImageList.Images.Add(lp.RelatedFile, icon.ToBitmap());

                }
            }
        }

        public static TreeNode GetTreeNode(string path)
        {
            if (path.StartsWith("/"))
                path = path.Substring(1);

            string[] parts = path.Split('/');
            TreeNode curNode = LuaListView.rootnode;
            string partpath = "";
            foreach (string part in parts)
            {
                partpath += part;
                while (curNode.FullPath != partpath)
                {
                    if (curNode.Nodes != null)
                    {
                        foreach (TreeNode tn in curNode.Nodes)
                        {
                            if (tn.FullPath == partpath) { curNode = tn; break; }
                        }
                    }
                    if (curNode.FullPath != partpath)
                    {
                        if (curNode.NextNode != null)
                            curNode = curNode.NextNode;
                        else
                            break;
                    }

                }

                partpath += "\\";
            }
            return curNode;
            
        }

    }
}
